package fr.imag.ufrima.ctte.gui.views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import sun.swing.ImageCache;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.TaskDiagram;
import fr.imag.ufrima.ctte.events.ImagCtteAdapter;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;

public class DiagramPropertiesSubView extends SubView {		
	private TaskDiagram taskDiagram;
	/**
	 * Constructor
	 */
	public DiagramPropertiesSubView(ImagCTTE imagCTTE){
		super(imagCTTE, "Diagram Properties");				
		InitComponents();		
		setTaskDiagram(imagCTTE.getCurrentTaskDiagram());
	}
	public TaskDiagram getTaskDiagram() {
		return taskDiagram;
	}
	public void setTaskDiagram(TaskDiagram taskDiagram) {
		this.taskDiagram = taskDiagram;
		if (taskDiagram != null) {
			txtDiagramName.setText(taskDiagram.getTitle());
			txtWidth.setText(String.valueOf(taskDiagram.getWidth()));
			txtHeight.setText(String.valueOf(taskDiagram.getHeight()));
		}
	}
	/**
	 * Initialize componenets
	 */
	protected void InitComponents(){

		lblDiagramName = new javax.swing.JLabel();
        txtDiagramName = new javax.swing.JTextField();
        txtDiagramName.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtDiagramName.getText();
        		txt.trim();
        		if (txt.equals("") != true){
        			getImagCTTE().getCurrentTaskDiagram().setTitle(txtDiagramName.getText());
        			txtDiagramName.setText(txt);
        		}
        	}
        });
        lblWidth = new javax.swing.JLabel();
        lblHeight = new javax.swing.JLabel();
        txtWidth = new javax.swing.JTextField();
        txtWidth.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtWidth.getText();
        		txt.trim();
        		int width = Integer.parseInt(txt);
        		if (width > 0) {        		
        			getImagCTTE().getCurrentTaskDiagram().setWidth(width);
        		}
        	}
        });

        txtHeight = new javax.swing.JTextField();
        txtHeight.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent arg0) {
        		String txt = txtHeight.getText();
        		txt.trim();
        		int height = Integer.parseInt(txt);
        		if (height > 0) {        		
        			getImagCTTE().getCurrentTaskDiagram().setHeight(height);
        		}
        	}
        });

        lblPixels1 = new javax.swing.JLabel();
        lblPixels2 = new javax.swing.JLabel();

        lblDiagramName.setText("Diagram name:");

        lblWidth.setText("Width:");

        lblHeight.setText("Height:");

        lblPixels1.setText("pixels");

        lblPixels2.setText("pixels");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblHeight)
                    .addComponent(lblWidth)
                    .addComponent(lblDiagramName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtDiagramName, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtWidth, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPixels1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtHeight, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPixels2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDiagramName)
                    .addComponent(txtDiagramName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblWidth)
                    .addComponent(txtWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPixels1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHeight)
                    .addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPixels2))
                .addContainerGap(232, Short.MAX_VALUE))
        );
	}
	/**
	 * Components
	 */
	private javax.swing.JLabel lblDiagramName;
    private javax.swing.JLabel lblHeight;
    private javax.swing.JLabel lblPixels1;
    private javax.swing.JLabel lblPixels2;
    private javax.swing.JLabel lblWidth;
    private javax.swing.JTextField txtDiagramName;
    private javax.swing.JTextField txtHeight;
    private javax.swing.JTextField txtWidth;
    	
}
